package com.chendx.article.kafka.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chendx.article.service.ApArticleConfigService;
import com.chendx.common.constants.message.WmNewsMessageConstants;
import com.chendx.model.article.pojos.ApArticleConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

/**
 * Created by chendx on 2022/3/7.
 */
@Component
public class ArticleIsDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMessage(ConsumerRecord<?, ?> record) {
        System.out.println("receiveMessage");
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()) {
            String value = (String) record.value();
            Map map = JSON.parseObject(value, Map.class);
            System.out.println(JSON.toJSONString(map));
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .eq(ApArticleConfig::getArticleId, map.get("articleId")).set(ApArticleConfig::getIsDown, map.get("enable")));
        }
    }
}
